

<template>
  <div>
    <!-- <Button type="primary" @click="modal1 = true">Display dialog box</Button> -->
    <Modal
      v-model="mode"
      width="60%"
      title="红枣质押-过磅"
      @change="testChange"
      @on-cancel="cancel"
    >
      <Form
        ref="EntryOrderData"
        :model="EntryOrderData"
        :rules="ruleInUser"
        :label-width="100"
      >
        <div style="width: 90%; height: auto; margin: 0 auto">
          <Row>
            <i-col span="24">
              <FormItem label="订单编号：" prop="Id">
                <Input
                  v-model="EntryOrderData.OrderNo"
                  class="orders_disabled"
                  placeholder="请输入用户编号"
                  disabled
                />
              </FormItem>
              <FormItem label="客户名称：" prop="ClassName">
                <label slot="label">客户名称：</label>
                <Input
                  v-model="EntryOrderData.CustomerName"
                  class="orders_disabled_custom"
                  placeholder="请输入客户名称"
                  disabled
                />
              </FormItem>
            </i-col>
          </Row>
          <Row>
            <i-col span="8">
              <FormItem label="毛重时间：" prop="RoughWeightTime">
                <Input
                  v-model="EntryOrderData.RoughWeightTime"
                  class="orders_disabled"
                  placeholder="无毛重时间"
                  style="width: 200px"
                  disabled
                />
              </FormItem>

              <FormItem label="毛重(公斤)：" prop="RoughWeight">
                <InputNumber
                  v-model="EntryOrderData.RoughWeight"
                  class="orders_disabled"
                  placeholder="请输入质押件数"
                  style="width: 100px"
                />
              </FormItem>

              <FormItem
                label="入厂车牌："
                prop="VehicleNo"
                v-if="EntryOrderData.Status == 1"
              >
                <Input
                  v-model="EntryOrderData.VehicleNo"
                  class="orders_disabled"
                  placeholder="请输入车牌号"
                  style="width: 100px"
                />
              </FormItem>

              <FormItem
                label="出厂车牌："
                prop="OutVehicleNo"
                v-if="EntryOrderData.Status == 4"
              >
                <Input
                  v-model="EntryOrderData.OutVehicleNo"
                  class="orders_disabled"
                  placeholder="请输入车牌号"
                  style="width: 100px"
                />
              </FormItem>
            </i-col>

            <i-col span="8">
              <FormItem label="皮重时间：" prop="SkinWeightTime">
                <Input
                  v-model="EntryOrderData.SkinWeightTime"
                  class="orders_disabled"
                  placeholder="无皮重时间"
                  style="width: 200px"
                />
              </FormItem>

              <FormItem label="皮重(公斤)：" prop="SkinWeight">
                <InputNumber
                  v-model="EntryOrderData.SkinWeight"
                  @on-change="weightChange"
                  class="orders_disabled"
                  style="width: 100px"
                />
              </FormItem>

              <FormItem label="拉运人员：" prop="Carrier">
                <Input
                  v-model="EntryOrderData.Carrier"
                  class="orders_disabled"
                  placeholder="请输入拉运人"
                  style="width: 100px"
                />
              </FormItem>
            </i-col>

            <i-col span="8">
              <FormItem label="占空位" prop="">
                <label slot="label">&nbsp;&nbsp;&nbsp;&nbsp;</label>
              </FormItem>

              <FormItem label="净重(公斤)：" prop="NetWeight">
                <InputNumber
                  v-model="EntryOrderData.NetWeight"
                  class="orders_disabled"
                  placeholder="请输入质押件数"
                  style="width: 100px"
                  disabled
                />
              </FormItem>

              <FormItem label="拉运电话：" prop="CarrierTelephone">
                <Input
                  v-model="EntryOrderData.CarrierTelephone"
                  class="orders_disabled"
                  placeholder="请输入电话号码"
                  style="width: 100px"
                />
              </FormItem>
            </i-col>
          </Row>

          <Row>
            <i-col span="24">
              <FormItem>
                <Button
                  type="primary"
                  :loading="loading"
                  class="buttn_rough"
                  @click="send()"
                  >读取重量</Button
                >
                <Button
                  type="primary"
                  :loading="loading"
                  class="buttn_rough"
                  v-if="EntryOrderData.Status == 1"
                  @click="handleSubmit('EntryOrderData', 2)"
                  >称毛重</Button
                >
                <Button
                  type="warning"
                  :loading="loading"
                  class="buttn_skin"
                  v-if="EntryOrderData.Status == 4"
                  @click="handleSubmit('EntryOrderData', 5)"
                  >称皮重</Button
                >
                <!-- <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button> -->
              </FormItem>
            </i-col>
          </Row>
        </div>
      </Form>

      <div slot="footer">
        <!-- <Button type="error" size="large" long :loading="modal_loading" @click="del">Delete</Button> -->
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  getCompanyList,
  getDeptList,
  updateEntryOrderData,
  timestampToDate,
} from "@/api/data";
import { formatDate } from "../../components/date.js";
import Cookies from "js-cookie";
import { getToken } from "@/libs/util";
import { Message } from "iview";
export default {
  name: "Edit",
  data() {
    return {
      //websocket连接本地插件地址
      path: "ws://127.0.0.1:8086/ud",
      socket: "",
      userName: "",
      userPassword: "",
      key: "",

      loading: false,
      mode: this.edit,

      //表单验证规则
      ruleInUser: {
        // UnitPrice: [
        //   { required: true, type:'number',min:2.0, message: '请填写大于或等于2.0的红枣单价', trigger: 'blur' }
        // ],

        VehicleNo: [
          { required: true, message: "请填写车牌号码", trigger: "blur" },
          {
            pattern: /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)/,
            message: "请输入正确的车牌号",
            trigger: "blur",
          },
        ],

        OutVehicleNo: [
          { required: true, message: "请填写车牌号码", trigger: "blur" },
          {
            pattern: /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)/,
            message: "请输入正确的车牌号",
            trigger: "blur",
          },
        ],

        CarrierTelephone: [
          { required: true, message: "请输入拉运人手机号码", trigger: "blur" },
          {
            pattern: /^1(3|4|5|6|7|8|9)\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
      },

      listdata: [],
    };
  },
  props: {
    edit: {
      type: Boolean,
      required: true,
    },
    EntryOrderData: {
      type: Object,
      required: true,
    },
  },
  methods: {
    //websocket初始化
    init: function () {
      if (typeof WebSocket === "undefined") {
        alert("您的浏览器不支持socket");
      } else {
        // 实例化socket
        this.socket = new WebSocket(this.path);
        // 监听socket连接
        this.socket.onopen = this.open;
        // 监听socket错误信息
        this.socket.onerror = this.error;
        // 监听socket消息
        this.socket.onmessage = this.getMessage;
        //监听socket关闭
        this.socket.onclose = this.close;

        console.log("websocket初始化");
      }
    },

    //监听websocket连接
    open: function () {
      console.log("socket连接成功");
    },
    //监听websocket错误信息
    error: function () {
      Message.error("连接地磅插件失败");
      console.log("连接错误");
    },
    //监听websocket消息
    getMessage: function (msg) {
      this.key = msg.data;
      if (this.key != "failure") {
        //this.handleSubmit({userName:this.userName,userPassword:this.userPassword,key:this.key})
        console.log("读取地磅成功!");
      } else {
        Message.error("读取地磅失败!");
      }
      //console.log(this.key)
    },
    //发送消息
    send: function () {
      // 初始化websocket
      this.init();
      let that = this;
      setTimeout(function () {
        that.socket.send("read");
        that.socket.close();
      }, 100);
    },
    //关闭websocket
    close: function () {
      console.log("socket已经关闭");
    },

    //销毁
    destroyed() {
      // 销毁监听
      this.socket.onclose = this.close;
    },

    //重量值改变动态修改净重
    weightChange() {
      var skinWeight = this.EntryOrderData.SkinWeight;
      var roughWeight = this.EntryOrderData.RoughWeight;
      var stat = this.EntryOrderData.Status;
      if (stat == 4) {
        this.EntryOrderData.NetWeight = roughWeight - skinWeight;
      }
    },
    testChange(val) {
      this.$emit("testChange", val);
    },
    cancel() {
      console.log("取消按钮");
      //this.socket.close()
      this.loading = false;
      let data = {
        mode: false,
      };
      this.$emit("cancelCallBack", data);
    },
    //提交编辑后的userInfo
    handleSubmit(name, status) {
      this.$refs[name].validate((valid) => {
        console.log("提交验证：", valid);
        if (valid) {
          this.loading = true;
          this.submit(status);
        } else {
          this.$Message.error("请填写相关信息!");
        }
      });
    },
    submit(status) {
      var skinWeightTime = this.EntryOrderData.SkinWeightTime;
      var roughWeightTime = this.EntryOrderData.RoughWeightTime;
      var roughWeightPerson = this.EntryOrderData.RoughWeightPerson;
      var skinWeightPerson = this.EntryOrderData.SkinWeightPerson;

      if (status == 5) {
        //出厂---皮重时间
        skinWeightTime = timestampToDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        skinWeightPerson = getToken();
      } else if (status == 2) {
        //毛重时间
        roughWeightTime = timestampToDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        roughWeightPerson = getToken();
      }
      const data = {
        Id: this.EntryOrderData.Id,
        OrderNo: this.EntryOrderData.OrderNo,
        ApplyTime: this.EntryOrderData.ApplyTime,
        CustomerName: this.EntryOrderData.CustomerName,
        LinkTelephone: this.EntryOrderData.LinkTelephone,
        BankName: this.EntryOrderData.BankName,
        BankAccount: this.EntryOrderData.BankAccount,
        Carrier: this.EntryOrderData.Carrier,
        CarrierTelephone: this.EntryOrderData.CarrierTelephone,
        VehicleNo: this.EntryOrderData.VehicleNo,
        RoughWeightPerson: roughWeightPerson,
        RoughWeightTime: roughWeightTime,
        RoughWeight: this.EntryOrderData.RoughWeight,
        SkinWeightPerson: skinWeightPerson,
        SkinWeightTime: skinWeightTime,
        SkinWeight: this.EntryOrderData.SkinWeight,
        NetWeight: this.EntryOrderData.NetWeight,
        CheckPerson: this.EntryOrderData.CheckPerson,
        CheckTime: this.EntryOrderData.CheckTime,

        //品种1信息
        CheckTypeFstId: this.EntryOrderData.CheckTypeFstId,
        CheckTypeFst: this.EntryOrderData.CheckTypeFst,
        CheckFstCount: this.EntryOrderData.CheckFstCount,
        CheckFstPrice: this.EntryOrderData.CheckFstPrice,
        CheckFstDlvCount: this.EntryOrderData.CheckFstDlvCount,
        CheckFstRemCount: this.EntryOrderData.CheckFstCount,

        //品种2信息
        CheckTypeSecId: this.EntryOrderData.CheckTypeSecId,
        CheckTypeSec: this.EntryOrderData.CheckTypeSec,
        CheckSecCount: this.EntryOrderData.CheckSecCount,
        CheckSecPrice: this.EntryOrderData.CheckSecPrice,
        CheckSecDlvCount: this.EntryOrderData.CheckSecDlvCount,
        CheckSecRemCount: this.EntryOrderData.CheckSecCount,

        //品种3信息
        CheckTypeThdId: this.EntryOrderData.CheckTypeThdId,
        CheckTypeThd: this.EntryOrderData.CheckTypeThd,
        CheckThdCount: this.EntryOrderData.CheckThdCount,
        CheckThdPrice: this.EntryOrderData.CheckThdPrice,
        CheckThdDlvCount: this.EntryOrderData.CheckThdDlvCount,
        CheckThdRemCount: this.EntryOrderData.CheckThdCount,

        //装箱类型
        CheckPackId: this.EntryOrderData.CheckPackId,
        CheckPack: this.EntryOrderData.CheckPack,
        UnitWeight: this.EntryOrderData.UnitWeight,

        //检验信息
        SugarRate: this.EntryOrderData.SugarRate,
        ImpurityRate: this.EntryOrderData.ImpurityRate,
        WaterRate: this.EntryOrderData.WaterRate,
        InsectRate: this.EntryOrderData.InsectRate,

        //入库人员及时间
        StorePerson: this.EntryOrderData.StorePerson,
        StoreTime: this.EntryOrderData.StoreTime,

        //品种1入库信息
        LandingPlaceFstId: this.EntryOrderData.LandingPlaceFstId,
        LandingPlaceFst: this.EntryOrderData.LandingPlaceFst,

        //品种2入库信息
        LandingPlaceSecId: this.EntryOrderData.LandingPlaceSecId,
        LandingPlaceSec: this.EntryOrderData.LandingPlaceSec,

        //品种3入库信息
        LandingPlaceThdId: this.EntryOrderData.LandingPlaceThdId,
        LandingPlaceThd: this.EntryOrderData.LandingPlaceThd,

        //结算信息
        AccountWeight: this.EntryOrderData.AccountWeight,
        SumMoney: this.EntryOrderData.SumMoney,

        Status: status,
      };
      var submitStatus = true;
      if (
        status == 5 &&
        this.EntryOrderData.VehicleNo != this.EntryOrderData.OutVehicleNo
      ) {
        submitStatus = false;
        this.loading = false;
        this.$Message.error("出厂和入厂车牌不一致！");
      }
      if (this.EntryOrderData.NetWeight < 0) {
        submitStatus = false;
        this.loading = false;
        this.$Message.error("皮重大于毛重，请检查！");
      }

      //判断是否符合提交条件
      if (submitStatus) {
        updateEntryOrderData(data)
          .then((res) => {
            if (res == "OK") {
              this.$Message.success("提交成功");
              this.loading = false;
              let data = {
                mode: false,
              };
              this.$emit("success", data);
            } else if (response.data.flag == 0) {
              this.$Message.success("提交失败");
            } else if (response.data.flag == 2) {
              this.$Message.success("状态过期");
            }
          })
          .catch((error) => {
            this.$Message.error("请核对之后重新提交1");
            this.loading = false;
          });
      }
    },
  },
  mounted() {
    // 初始化websocket
    //this.init()
    //请求参数
    const cmpdata = {
      fields: "Id,CompanyName",
      offset: "0",
      limit: "-1",
    };
    const dptdata = {
      fields: "Id,DeptName",
      offset: "0",
      limit: "-1",
    };
    //获得包含公司Id和companyname字段的全部列表
    // getCompanyList(cmpdata).then(res =>{
    //   this.CompanyList = res.List;

    // });
    //获得包含公司Id和companyname字段的全部列表
    // getDeptList(dptdata).then(res =>{
    //   this.DeptList = res.List;
    // });
  },
  watch: {
    edit(val) {
      this.mode = val;
    },
  },
};
</script>
<style>
.dept .ivu-select-placeholder {
  color: #515a6e !important;
}

.orders_disabled_custom input {
  border: none;
  background-color: #fff !important;
  color: gray !important;
  width: 280px;
}

.buttn_rough {
  width: 100px;
}

.buttn_skin {
  width: 100px;
  margin-left: 150px;
}
</style>